package com.cherrish.demo.algorithm.sort;

/**
 * @author cherrish
 * @time 2019-02-22 15:01
 * @name SortInsertion
 * @desc:
 */
public class SortInsertion implements ISort{
    @Override
    public void sort(int[] arr, int len) {
//        sort1(arr, len);
        sort2(arr, len);
    }

    public void sort2(int[] arr, int len) {
        for (int i = 1; i < len; i++) {
            int num = arr[i];
            int j = i;
            for (; j > 0 && num < arr[j - 1]; j--) {
                arr[j] = arr[j - 1];
            }
            arr[j] = num;
        }
    }

    public void sort1(int[] arr, int len) {
        for(int i = 0; i < len - 1; i++){
            for(int j = i + 1; j > 0; j--){
                if(arr[j] < arr[j - 1]){
                    swap1(arr, j, j - 1);
                }
            }
        }
    }
}
